package uk.org.wookey.jpet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class MySqlDb {
	private static Connection dbConnection = null;
	private static String path;
	private static String user;
	private static String password;

	public MySqlDb(String path, String user, String password) {
		openDb(path, user, password);
	}
	
	public MySqlDb() throws IOException {
		if (dbConnection == null) {
			throw new IOException("Database details not known.");
		}
	}
	
	private void openDb(String path, String user, String password) {
		boolean different = false;
		
		System.out.println("openDb(" + path + ", " + user + ", " + password + ")");
		
		if (dbConnection != null) {
			if (!this.path.equalsIgnoreCase(path)) {
				different = true;
			}
			else if (!this.user.equalsIgnoreCase(user)) {
				different = true;
			}
			else if (!this.password.equalsIgnoreCase(password)) {
				different = true;
			}
			
			if (different) {
				try {
					dbConnection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				dbConnection = null;
			}
		}
		
		// Load the MySQL driver...
		if (dbConnection == null) {
			try {
				Class.forName("com.mysql.jdbc.Driver");			

				String dbUrl = "jdbc:mysql://" + path + "?user=" + user + "&password=" + password;
				System.out.println("DB URL: " + dbUrl);
				
				dbConnection = DriverManager.getConnection(dbUrl);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
		}
	}
	
	public Connection getConnection() {
		return dbConnection;
	}
	
	public String[] getTableNames() {
		ArrayList<String> names = new ArrayList<String>();
		
		DatabaseMetaData md;
		
		try {
			md = dbConnection.getMetaData();

			ResultSet rs = md.getTables(null, null, "%", null);
			while (rs.next()) {
			  names.add(rs.getString("TABLE_NAME"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String[] res = new String[names.size()];
		res = names.toArray(res);
		
		return res;
	}
	
	public String[] getColumnNames(String tableName) {
		ArrayList<String> names = new ArrayList<String>();
		
		DatabaseMetaData md;
		
		try {
			md = dbConnection.getMetaData();

			ResultSet rs = md.getColumns(null, null, tableName, null);
			while (rs.next()) {
				DbColumn col = new DbColumn(tableName, rs.getString("COLUMN_NAME"), 
							rs.getString("TYPE_NAME") + "(" + rs.getInt("DATA_TYPE") + ")");
				names.add(col.getDeclaration());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String[] res = new String[names.size()];
		res = names.toArray(res);
		
		return res;
	}
	
	public void tableInfo(String tableName) {
		
	}
} 